from sanic import response

from model.deviceModel import search_swinery_link_dev, search_dev, search_user_link_dev, insert_swinery_link_dev, \
        delete_swinery_link_dev, update_dev, judge_a_device
from salesmanModel import judge_a_staff
from model.userModel import select


async def swinery_dev_list(request):
        swinery_id = request.form.get('swinery_id')

        # 得到该猪栏的所有设备
        devInfo = search_swinery_link_dev(swinery_name=swinery_id)
        devID_list = [devInfo[i]['device_id'] for i in range(len(devInfo))]

        # 得到设备的操作人员
        for i in range(len(devInfo)):
                # 该台设备
                device_id = devID_list[i]
                # 得到饲养人员
                users_dev = search_user_link_dev(device_id=device_id)   # 所有与这台设备有关的人
                devUser_list = [users_dev[i]['user_id'] for i in range(len(users_dev))]
                operator = None
                for user in devUser_list:
                        he = judge_a_staff(user_id=user, role_id=4)     # 4-D-饲养员
                        # <<<<<<<<<<只有一个操作人员吗？
                        if he:
                                operator = select(id=user)['username']
                devInfo[i]['operator'] = operator       # 添加操作人员

                # 得到设备状态
                dev_status = search_dev(device_id=device_id)[0]['status']
                devInfo[i]['status'] = dev_status       # 添加设备状态

                # 得到设备号
                devSN = search_dev(device_id=devID_list[i])[0]['deviceSN']
                devInfo[i]['deviceSN'] = devSN

        return response.json(devInfo)


async def swinery_add_dev(request):
        swinery_id = request.form.get('swinery_id')
        device_id = request.form.get('device_id')

        bingo = judge_a_device(status='C', device_id=device_id)
        if not bingo:
                return response.text('设备不合理！')

        # 插入该关系
        # 更改设备状态
        success1 = insert_swinery_link_dev(swinery_id=swinery_id, device_id=device_id)
        success2 = update_dev(status='D', device_id=device_id)
        if success1 and success2:
                return response.text('猪栏设备添加成功！')
        else:
                # 回滚
                delete_swinery_link_dev(swinery_name=swinery_id, device_id=device_id)
                update_dev(status='C', device_id=device_id)
                return response.text('猪栏设备添加失败！')


async def swinery_delete_dev(request):
        swinery_id = request.form.get('swinery_id')
        device_id = request.form.get('device_id')

        bingo = judge_a_device(status='D', device_id=device_id)         # <<<<<<<<<<<<<<<<<<<<需要对应D, D1, D2??
        if not bingo:
                return response.text('设备不合理！')

        # 删除关系
        # 更改设备状态
        success1 = delete_swinery_link_dev(swinery_name=swinery_id, device_id=device_id)
        success2 = update_dev(status='C', device_id=device_id)
        if success1 and success2:
                return response.text('猪栏设备删除成功！')
        else:
                # 回滚
                insert_swinery_link_dev(swinery_id=swinery_id, device_id=device_id)
                update_dev(status='D', device_id=device_id)
                return response.text('猪栏设备删除失败！')
